package logic

import (
	"context"
	"everyone/C/rpc_C/model/mysql"
	"everyone/C/rpc_C/model/redis"
	"everyone/C/rpc_C/pkg"

	"everyone/C/rpc_C/everyone"
	"everyone/C/rpc_C/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *everyone.UserLoginRequest) (*everyone.UserLoginResponse, error) {
	// todo: add your logic here and delete this line
	get := redis.Client.Get(context.Background(), "Smslogin"+in.Phone)
	if get.Val() != in.Code {
		logx.Error("当前验证码错误")
	}
	var find mysql.User
	mysql.DB.Find(&find)
	if find.ID == 0 {
		logx.Error("当前账号未被注册")
	}
	if find.Password != pkg.Md5Password(in.Password) {
		logx.Error("当前密码错误")
	}
	redis.Client.Del(context.Background(), "Smslogin"+in.Phone)
	return &everyone.UserLoginResponse{
		UserId: int64(find.ID),
	}, nil
}
